<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
use app\admin\model\Address as Address_Model;
/**
 * 收货地址接口
 */
class Address extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];
    protected $LitestoreGoods = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->user_id = $this->auth->id;
    }  

    /**
     * 获取省市区 
     */
    public function shengshiqu(){
        $shengshiqu = Db::name('area')->field("id,pid,name,1 as level")->where('pid',0)->select();
        
        foreach ($shengshiqu as $k => $v) {
            $city = Db::name('area')->field("id,pid,name,2 as level")->where('pid',$v['id'])->select();
            // $shengshiqu[$k]['city'] = $city;
            foreach ($city as $kk => $vv) {
                $city[$kk]['area']=Db::name('area')->field("id,pid,name,3 as level")->where('pid',$vv['id'])->select();
            }
            $shengshiqu[$k]['city'] = $city;
            
        }
        $this->success('获取省市区',$shengshiqu);
    }
    
    /**
     * 我的收货地址列表 isdefault等于1为默认地址
     */
    public function address_list(){
      $data = Db::name('address')->where(['user_id'=>$this->user_id])->order('isdefault desc,updatetime desc')->select();
      $this->success('收货地址列表',$data);
    }

    /**
     * 收货地址详情
     * @param int $address_id
     */
    public function address_detail(){
      $data = Db::name('address')->where(['id'=>input('address_id')])->find();
      $this->success('收货地址详情',$data);
    }

     /**
     * 收货地址添加,修改,删除以及设置默认收货地址。
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/address/action_address)
     * @ApiParams   (name="phone",type=int,required=false,description="手机号码")
     * @ApiParams   (name="name",type=string,required=false,description="收货人姓名")
     * @ApiParams   (name="detail",type=string,required=false,description="详细地址")
     * @ApiParams   (name="isdefault",type=int,required=false,description="是否默认:0=非默认,1=默认地址")
     * @ApiParams   (name="province",type=int,required=false,description="省")
     * @ApiParams   (name="city",type=string,required=false,description="市")
     * @ApiParams   (name="region",type=int,required=false,description="区")
     * @ApiParams   (name="area_id",type=int,required=true,description="第二级别的id")
     * @ApiParams   (name="address_id",type=int,required=false,description="收货地址id")
     * @ApiParams   (name="action",type=string,required=true,description="添加时候为add,修改和设置默认时候为edit,删除时候传del")
     */
    public function action_address(){
        
        $params = input();
        $params['user_id'] = $this->user_id;
        $action = input('action');

        if($action=='edit'){//修改地址
            $Address = new Address_Model();
            $address_id = $params['address_id'];
            unset($params['address_id']);
            unset($params['action']);
            if(array_key_exists('isdefault', $params))
            {    
                if($params['isdefault']==1){//修改默认地址
                    Db::name('address')->where(['user_id'=>$this->user_id])->update(['isdefault'=>0]);

                    $Address->allowField(true)->where('id',$address_id)->update($params);
                }else{
                    $Address->allowField(true)->where('id',$address_id)->update($params);
                }

            }else{//修改收获地址信息
                    $Address->allowField(true)->where('id',$address_id)->update($params);
                }
        }
        if($action=='add'){//添加地址
            $exist_data = Db::name('address')->where(['user_id'=>$this->user_id])->find();
            if(array_key_exists('isdefault', $params)){
                if($params['isdefault']==1){
                    Db::name('address')->where('user_id',$this->user_id)->update(['isdefault'=>0]);
                }
            }
          if(empty($exist_data))$params['isdefault']=1;       
          unset($params['action']);
          unset($params['address_id']);
          $Address = new Address_Model($params);
          $Address->allowField(true)->save();
        }

        if($action=='del'){
            $data = Db::name('address')->where(['id'=>input('address_id')])->delete();
            if($data)$this->success('操作成功');
        }


        $this->success('操作成功');

    }


     

    
  

}
